﻿using MockNetCore.Handlers;
using System;

namespace MockNetCore
{
    public class Handler
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="template">数据模版</param>
        /// <param name="name">属性名称</param>
        /// <param name="context">数据上下文，生成后的数据</param>
        /// <returns></returns>
        public static Object gen(Object template, string name, Context context)
        {
            if (string.IsNullOrEmpty(name)) name = "";
            if (context == null) context = new Context();
            Rule rule = Parser.parseRule(name);
            Type type = Parser.parseType(template);
            Options options = new Options()
            {
                template = template,
                context = context,
                name = name,
                parsedName = parseName(name),
                rule = rule,
                type = type
            };
            return TypeHandlerFactory.getTypeHandler(type).handle(options);
        }

        private static String parseName(String name)
        {
            if (name.Contains("|"))
                return name.Split('|')[0];
            return name;
        }
    }
}